/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-05-24 17:33:00
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 16:17:22
 */


'use strict';
define([
    'jstep',
    '../framework/base/baseView',
    './createMaterial/step1/step1',
    './createMaterial/step2/step2',
    './createMaterial/step3/step3'
], function(
    jstep,
    BaseView,
    Step1View,
    Step2View,
    Step3View
) {
    function me() {
        BaseView.apply(this, arguments);
        var self = this;
        this.init = function() {
            self.$root = $('.fmp-container');
            self.step1View = null;
            self.step2View = null;
            self.step3View = null;
            self.render();
        }

        this.render = function() {
            var currentStep = self.kit.getCache('createMaterialJumpStep', 'currentStep');
            if (currentStep == null) {
                self.enterStep1();
            } else {
                showConfirm({
                    title:self.i18n('lastTitle'),
                    content: self.i18n('theLastUnfinishedCreation'),
                    onOK: function() {
                        if (currentStep == 1) {
                            self.enterStep1(null, true);
                        } else if (currentStep == 2) {
                            self.enterStep2(null, true);
                        } else if (currentStep == 3) {
                            self.enterStep3(null, true);
                        } else {
                            self.kit.alert2(self.i18n('exception'));
                        }
                    },
                    onCancel: function() {
                        self.clearCache();
                        self.enterStep1();
                    },
                    btnOK: self.i18n('submit'),
                    btnCANCEL:self.i18n('cancel')
                });
            }
            self.event();
        }

        this.event = function() {
            self.$root.off('clearCache').on('clearCache', $.proxy(self.clearCache, self));
            self.$root.off('enterStep2').on('enterStep2', $.proxy(self.enterStep2, self));
            self.$root.off('enterStep3').on('enterStep3', $.proxy(self.enterStep3, self));
        }

        /**
         * [enterStep1 description]
         * @param  {[type]} e       [description]
         * @param  {[type]} restore [是否从缓存中恢复数据]
         * @return {[type]}         [description]
         */
        this.enterStep1 = function(e, restore) {
            self.step1View = new Step1View();
            self.step1View.init(self.$root, self.jumpStep, restore);
        }

        /**
         * [enterStep2 description]
         * @param  {[type]} e       [description]
         * @param  {[type]} restore [是否从缓存中恢复数据]
         * @return {[type]}         [description]
         */
        this.enterStep2 = function(e, restore) {
            self.step2View = new Step2View();
            self.step2View.init(self.$root, self.jumpStep, restore);
        }

        /**
         * [enterStep3 description]
         * @param  {[type]} e       [description]
         * @param  {[type]} restore [是否从缓存中恢复数据]
         * @return {[type]}         [description]
         */
        this.enterStep3 = function(e, restore) {
            self.step3View = new Step3View();
            self.step3View.init(self.$root, self.jumpStep, restore);
        }


        this.jumpStep = function(step) {
            self.kit.setCache('createMaterialJumpStep', 'currentStep', step);
            $('.header-step').jstep({
                data: [self.i18n('creatingPreview'), self.i18n('generateIdeas'), self.i18n('savePreview')],
                start: step
            }); //插件不完整，只能使用3个
            $('.header-step').find('.ui-jstep-step').off('click').on('click', function(event) {
                if ($(this).find('.ui-jstep-step-tip').hasClass('done')) {
                    var step = $(this).data('step') * 1;
                    if (step == 1) {
                        self.enterStep1(null, true);
                    } else if (step == 2) {
                        self.enterStep2(null, true);
                    } else if (step == 3) {
                        self.enterStep3(null, true);
                    }
                }

            });
        }

        /**
         * [clearCache 清空相关缓存]
         * @return {[type]} [description]
         */
        this.clearCache = function() {
            window.localStorage.removeItem('createMaterialStep1');
            window.localStorage.removeItem('createMaterialJumpStep');
        }

        this.i18n = function(key) {
            return I18N.getText('createMaterial', key)
        }

    }
    return new me();
});